﻿using IOA.MES.Client.Common;
using IOA.MES.Client.Models;
using IOA.MES.Client.Models.Response;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace IOA.MES.Client.Top
{
    /// <summary>
    /// TopSetting.xaml 的交互逻辑
    /// </summary>
    public partial class TopSetting : Window
    {
        public TopSetting()
        {
            InitializeComponent();
        }

        private List<MachineAddressSetting> plcServers = new List<MachineAddressSetting>();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbFloor.ItemsSource = new List<string> { "上", "下" };
            InitMachines();
        }

        public void InitMachines()
        {
            var machines = BaseEnv.InvokeApi<QueryMachinesResponse>("/api/Machine/QueryMachines", new { WorkshopCode = BaseEnv.WorkshopCode, MachineType = MachineType.TopMachine }).Data.Machines;
            cbMachines.ItemsSource = machines;
            cbMachines.DisplayMemberPath = "MachineName";
            cbMachines.SelectedValuePath = "MachineId";

            if (machines.Any())
            {
                cbMachines.SelectedIndex = 0;
            }

            var opcServerJson = BaseEnv.GetStorage("TopPLCAddresses");
            plcServers = string.IsNullOrWhiteSpace(opcServerJson) ? new List<MachineAddressSetting>() : JsonConvert.DeserializeObject<List<MachineAddressSetting>>(opcServerJson);
            plcServers = plcServers.Where(opc => machines.Select(_ => _.MachineId).Contains(opc.MachineId)).ToList();
            gridAddresses.ItemsSource = plcServers;
            BaseEnv.SetStorage("TopPLCAddresses", JsonConvert.SerializeObject(plcServers));
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            var dot = ((Button)sender).DataContext as MachineAddressSetting;
            plcServers.Remove(dot);
            gridAddresses.ItemsSource = null;
            gridAddresses.ItemsSource = plcServers;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var machine = cbMachines.SelectedItem as Machine;
            if (plcServers.Any(_ => _.MachineId == machine.MachineId && string.Equals(_.Floor, Convert.ToString(cbFloor.SelectedValue), StringComparison.CurrentCultureIgnoreCase)))
            {
                CommonMessage.Alert(this, "层压机&层已存在");
                return;
            }
            var item = new MachineAddressSetting
            {
                MachineId = machine.MachineId,
                MachineName = machine.MachineName,
                Floor = Convert.ToString(cbFloor.SelectedValue),
                Address = txtAddress.Text.Trim()
            };
            plcServers.Add(item);
            plcServers.ForEach(_ => _.No = plcServers.IndexOf(_));
            gridAddresses.ItemsSource = null;
            gridAddresses.ItemsSource = plcServers;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            BaseEnv.SetStorage("TopPLCAddresses", JsonConvert.SerializeObject(plcServers));
            this.DialogResult = true;
            this.Close();
        }
    }
}
